package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class addressBookController {
    @Autowired
    private AddressBookService addressBookService;


    //新增地址
    @PostMapping
    public Result<AddressBook> add(@RequestBody AddressBook addressBook){

        log.info("新增地址{}",addressBook);

        AddressBook addressBook1 = addressBookService.add(addressBook);

        return Result.success(addressBook1);
    }

    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询所有地址");
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        log.info("查询默认地址，当前用户id为{}", BaseContext.getCurrentId());
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }

}
